const http = require('http')
const https = require('https')
http.createServer((req, res) => {
  const urll = new URL(req.url, 'http://localhost:3000/')

  res.writeHead(200, {
    "Cpntent-Type": "application/json;charset=utf-8",
    "access-control-allow-origin": '*'
  })
  console.log(urll.pathname)
  switch (urll.pathname) {
    case '/data':
      httpGet(dataStr => {
        res.end(dataStr)
      })
      // res.end()
      break;

    default:
      res.end('404')
  }
}).listen(3000, () => console.log('创建成功！'))

function httpGet (fn) {
  let dataStr = ''
  // https.get('https://ehall.hniu.cn/jsonp/serviceRoleApp.json?serviceRoleId=1__1&type=all&_=1675860987839',
  https.get('https://v2.api-m.com/api/meinvpic',
    res => {
      res.on('data', chunk => {
        dataStr += chunk
      })

      res.on('end', () => {
        console.log(dataStr)
        fn(dataStr)
        // fn.end(dataStr)
      })
    })

}